<?php

namespace app\admin\service;

use app\admin\model\Team;
use think\facade\Db;

class OneService extends BaseService
{
    public function __construct()
    {
        $this->model = new Team();
    }

    public function getList()
    {
        $param = request()->param();
        $map = [];

        // 名称
        $name = isset($param['name']) ? trim($param['name']) : '';
        if ($name) {
            $map[] = ['o.name', 'like', "%{$name}%"];
        }
        $map[] = ['o.mark', '=', 1];
        $map[] = ['o.indirect_id', '=', $param['id']];
        $list = Db::name('mini_room_list_distribution')
            ->alias('o')
            ->join('mini_room_list m', 'm.id=o.store_id')
            ->where($map)->order('o.create_time desc')->page(PAGE, PERPAGE)
            ->select();

        foreach ($list as $i => $info) {

            $roomlist = Db::name('mini_room_list')->where('id', $info['store_id'])->find();

            $info['referrer'] = '';
            $info['price'] = 0;
            if ($roomlist) {
                $info['referrer'] = $roomlist['room_name'];
                $infoprice = Db::name('distribution_price')->where('store_id', $info['direct_id'])->where('direct_id', $roomlist['id'])->sum('price');
                $info['price'] = $infoprice . '元';
            }
            $info['direct_name'] = '';
            $info['direct_price'] = 0;
            if ($info['indirect_id'] > 0) {


                $roomlist1 = Db::name('mini_room_list')->where('id', $info['indirect_id'])->find();

                if ($roomlist1) {
                    $info['direct_name'] = $roomlist1['room_name'];
                    $infoprice = Db::name('distribution_price')->where('store_id', $info['indirect_id'])->where('direct_id', $roomlist['id'])->sum('price');


                    $info['direct_price'] = $infoprice . '元';

                }
            }


            $info['second_name'] = '';
            $info['level_price'] = 0;
            if ($info['introduction_id'] > 0) {
                $roomlist2 = Db::name('mini_room_list')->where('id', $info['introduction_id'])->find();

                if ($roomlist2) {
                    $info['second_name'] = $roomlist2['room_name'];

                    $infoprice = Db::name('distribution_price')->where('store_id', $info['introduction_id'])->where('direct_id', $roomlist['id'])->sum('price');


                    $info['level_price'] = $infoprice . '元';

                }
            }
            $list[$i] = $info;
        }

        $count = Db::name('mini_room_list_distribution')
            ->alias('o')
            ->join('mini_room_list m', 'm.id=o.store_id')
            ->where($map)->count();
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;

    }
}